/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.common.scheduler.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.smart.common.enums.ReportTypeEnum;
import com.smart.common.enums.SmsTemplateEnum;
import com.smart.mapper.ProjectMapper;
import com.smart.mapper.ProjectTempTaskMapper;
import com.smart.model.domain.ProjectDO;
import com.smart.model.domain.ProjectTempTaskDO;
import com.smart.model.domain.ProjectWeeklyTaskDO;
import com.smart.utils.SmsUtil;
import com.sun.xml.bind.marshaller.Messages;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 * @version SmsScheduleService.java, v 0.1 2023/9/25 14:06 下午 SmsScheduleService Exp $$
 */
@Slf4j
@Component
public class TempTaskScheduleSmsService {

    @Autowired
    private ProjectTempTaskMapper projectTempTaskMapper;
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 触发条件，每天上午10点，扫描有今天开始的任务还有未提报的则短信提醒
     * @return
     */
    public boolean tempTaskStart() {
        Date date = new Date();
        List<ProjectTempTaskDO> projectTempTasks = projectTempTaskMapper.listByStartTime(DateUtil.beginOfDay(date).toJdkDate());
        if (CollectionUtils.isEmpty(projectTempTasks)) {
            log.info("当天不存在开始临时任务，无需提醒");
            return true;
        }

        LinkedHashMap<String, String> messages = new LinkedHashMap<>();
        messages.put("time", String.format("%s 18:00", DateUtil.format(date, DatePattern.NORM_DATE_PATTERN)));

        for (ProjectTempTaskDO projectTempTaskDO : projectTempTasks) {
            List<ProjectDO> projects = projectMapper.listUnReportByReportIdAndItemType(projectTempTaskDO.getId(), ReportTypeEnum.TEMP.name());
            if (CollectionUtils.isEmpty(projects)) {
                continue;
            }

            for (ProjectDO projectDO : projects) {
                SmsUtil.sendMessage(Lists.newArrayList(projectDO.getShigongContactPhone()), SmsTemplateEnum.SMS_463166300, messages);
            }
        }
        return true;
    }

    /**
     * 触发条件，每周四下午2点，本周有周报但是还未提报的短信提醒
     * @return
     */
    public boolean tempTaskEnd() {
        Date date = new Date();
        String dateStr = DateUtil.format(date, DatePattern.NORM_DATE_PATTERN);
        List<ProjectTempTaskDO> projectTempTasks = projectTempTaskMapper.listByEndTime(DateUtil.parse(dateStr + " 23:59:59", DatePattern.NORM_DATETIME_PATTERN));
        if (CollectionUtils.isEmpty(projectTempTasks)) {
            log.info("当天不存在结束临时任务，无需提醒");
            return true;
        }

        LinkedHashMap<String, String> messages = new LinkedHashMap<>();
        messages.put("time", String.format("%s 18:00", dateStr));

        for (ProjectTempTaskDO projectTempTaskDO : projectTempTasks) {
            List<ProjectDO> projects = projectMapper.listUnReportByReportIdAndItemType(projectTempTaskDO.getId(), ReportTypeEnum.TEMP.name());
            if (CollectionUtils.isEmpty(projects)) {
                continue;
            }

            for (ProjectDO projectDO : projects) {
                SmsUtil.sendMessage(Lists.newArrayList(projectDO.getShigongContactPhone(), projectDO.getShigongHeadPhone()), SmsTemplateEnum.SMS_463191286, messages);
            }
        }
        return true;
    }
}
